package com.hubery.designpattern.design.chain;

import com.hubery.designpattern.enums.ChainBizTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: lxy
 * @date: 2023/2/28 00:04
 * @Description:
 */
@Slf4j
@Component
public class ChainSupport {

    @Resource
    private List<IChain> chainList;

    private Map<ChainBizTypeEnum, IChain> chainMap = new EnumMap<>(ChainBizTypeEnum.class);

    @PostConstruct
    public void init() {
        chainMap = chainList.stream().collect(Collectors.toMap(IChain::getChainBizTypeEnum, Function.identity()));
    }

    public IChain getChainService(ChainBizTypeEnum chainBizTypeEnum) {
        return chainMap.getOrDefault(chainBizTypeEnum, null);
    }
}
